package com.studyweb.mapper;

import com.studyweb.pojo.entity.Paper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PaperMapper {
    
    /**
     * 创建试卷
     */
    @Insert("INSERT INTO paper(name, total_duration, total_questions, create_time, update_time) " +
            "VALUES(#{name}, #{totalDuration}, #{totalQuestions}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Paper paper);
    
    /**
     * 根据ID查询试卷
     */
    @Select("SELECT * FROM paper WHERE id = #{id}")
    Paper selectById(Long id);
    
    /**
     * 查询所有试卷
     */
    @Select("SELECT * FROM paper ORDER BY create_time DESC")
    List<Paper> selectAll();
    
    /**
     * 根据条件查询试卷
     */
    @Select("<script>" +
            "SELECT * FROM paper " +
            "<where>" +
            "  <if test='name != null and name != \"\"'>" +
            "    name LIKE CONCAT('%', #{name}, '%')" +
            "  </if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<Paper> selectByCondition(@Param("name") String name);
    
    /**
     * 更新试卷
     */
    @Update("UPDATE paper SET name = #{name}, total_duration = #{totalDuration}, " +
            "total_questions = #{totalQuestions}, update_time = #{updateTime} WHERE id = #{id}")
    void update(Paper paper);
    
    /**
     * 删除试卷
     */
    @Delete("DELETE FROM paper WHERE id = #{id}")
    void deleteById(Long id);
}